$(function() {
    loadStudentList();
});

function loadStudentList(){
    $.ajax({
        url:"/api/student/list",
    }).done(function(data) {
        //console.log(data);
        let html= "";
        data.forEach((element,index) => {
            html+="<tr>"
            html+="<td>"+element.studentID+"</td>"
            html+="<td>"+element.name+"</td>"
            html+="<td>"+element.gender+"</td>"
            html+="<td>"+element.age+"</td>"
            html+="<td> <a href='#' onclick='showStudentDlg("+element.studentID
                +")'>编辑</a> &nbsp;&nbsp;<a href='#' onclick='deleteById("+element.studentID+")'>删除</a></td>"
            html+="</tr>"
        })
        $("#studentTb").html(html);
    });
}

let layerIndex;
function showStudentDlg(studentID){

    let title="新增学生"
    if (studentID){
        title="编辑学生"
        $.ajax({
            url:"/api/student/"+studentID,
            method: "GET",
        }).done(result=>{
            console.log(result)

            $.each(result,function (key,value){
                var field = $('#studForm [name="' + key + '"]');
                if(field.is(':radio')){
                    field.filter('[value="' + value + '"]').prop('checked',true);
                } else if (field.is(':checkbox')){
                    field.prop('checked', value==="yes");
                } else {
                    field.val(value);
                }
            });
        })
    }else{
        $("#studForm")[0].reset();
    }
    layerIndex=layer.open({
        type: 1,
        title:title,
        area: ['500px','auto'],
        content: $("#studForm")
    })
}
layui.use(function (){
    layui.form.on('submit(stud-dlg)',function(data){
        event.preventDefault();
        commitStuDlg();
    })
})
function commitStuDlg(){

    let studentID = $("#studentId").val();
    if(studentID!=null && studentID!==""){

        let formData = $("#studForm").serialize();
        $.ajax({
            url:"/api/student/update",
            method:"PUT",
            data: formData
        }).done(result=>{
            console.log(result);
            if(result.studentID){
                loadStudentList();
                console.log("add success")
                if(layerIndex){
                    layer.close(layerIndex);
                }
            }
        }).fail((jqXHR, textStatus, errorThrown) => {
            console.error("Request failed: " + textStatus + errorThrown);
            alert("An error occurred. Please try again.");
        });
    } else{
        let formData = $("#studForm").serialize();
        $.ajax({
            url:"/api/student/add",
            method:"POST",
            data: formData
        }).done(result=>{
            console.log(result);
            if(result.studentID){
                loadStudentList();
                console.log("add success")
                if(layerIndex){
                    layer.close(layerIndex);
                }
            }
        }).fail((jqXHR, textStatus, errorThrown) => {
            console.error("Request failed: " + textStatus + errorThrown);
            alert("An error occurred. Please try again.");
        });
    }

    $("#btnOK").prop("disabled", true).addClass("layui-btn-disabled");
}
function deleteById(studentID){
    loadStudentList();
    console.log("delete");
    layer.confirm('真的要删除吗？一旦删除，不可恢复', {icon: 3}, function(){

        $.ajax({
            url:"/api/student/delete/"+studentID,
            method:"DELETE",
        }).done(result=>{
            loadStudentList();
        })
        layer.closeAll();
    }, function(){

    });
}